home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / borland / prbgi097.zip / C.ZIP / TVBGI.CPP < prev    next >
Text File  |  1992-12-15  |  18KB  |  554 lines

  1. /*---------------------------------------------------------*/
  2. /*                                                         */
  3. /*   Turbo Vision 1.0                                      */
  4. /*   Turbo Vision BGI Support Demo                         */
  5. /*   Copyright (c) 1991 by Borland International           */
  6. /*                                                         */
  7. /*---------------------------------------------------------*/
  8. /*
  9.    Sorry, this is my first OOP (and Turbo Vision) program.
  10.    Andrzej Resztak
  11.    e-mail: Resztak@PLUMCS11.bitnet
  12.  */
  13. #define Uses_TKeys
  14. #define Uses_TApplication
  15. #define Uses_TEvent
  16. #define Uses_TRect
  17. #define Uses_TDialog
  18. #define Uses_TButton
  19. #define Uses_TMenuBar
  20. #define Uses_TSubMenu
  21. #define Uses_TMenuItem
  22. #define Uses_TStatusLine
  23. #define Uses_TStatusItem
  24. #define Uses_TStatusDef
  25. #define Uses_TDeskTop
  26. #define Uses_MsgBox
  27. #define Uses_TChDirDialog
  28. #define Uses_THistory
  29. #define Uses_TWindow
  30. #define Uses_TInputLine
  31. #define Uses_TLabel
  32. #define Uses_TSItem
  33. #define Uses_TCheckBoxes
  34. #define Uses_TCollection
  35. #define Uses_TSystemError
  36. #include <tv.h>
  37.  
  38. #include "Tvbgi.h"
  39. #include <stdlib.h>
  40. #include <graphics.h>
  41. #include <string.h>
  42. #include <strstrea.h>
  43. #include <alloc.h>
  44. #include <assert.h>
  45.  
  46. #include "tv2.h"
  47. #include "prtgraph.h"
  48. #include "BGIDEMO.H"
  49.  
  50. #if __BCPLUSPLUS__==0x0200
  51.    #error Sorry, this program seems to not work under BC++ 2.0.
  52.    #error I am new to OOP and Turbo Vision and don't know why.
  53.    #error The error has nothing common with PrintBGI package.
  54.    #error It just hangs up when (or after) choosing printer.
  55.    #error So if you want to use it under BC++ 2.0 change code
  56.           choosing printer.
  57. #endif
  58.  
  59.  
  60. unsigned _stklen=4096;
  61.  
  62. char pathToDrivers[MAXPATH] = "";  // Default location of *.BGI files
  63.  
  64. int _PRT__pascal TVUserPrtFunc( void far* UserPtr,
  65.                                 PRT__handleT   *handlePPtr,
  66.                                 const char far* BGIpath );
  67.  
  68. static int StdBGI;
  69.  
  70. /*================*/
  71. TBGIApp::TBGIApp() :
  72. /*================*/
  73.     TProgInit( &TBGIApp::initStatusLine,
  74.            &TBGIApp::initMenuBar,
  75.            &TBGIApp::initDeskTop )
  76. {   const char far *p=NULL;
  77.     if (*pathToDrivers==0) p = getenv("BGIPATH");
  78.     if ( p==NULL ) p = pathToDrivers;
  79.     strcpy(Options.bgiPath, p);
  80.     fexpand(Options.bgiPath);
  81.  
  82.     Options.PicWidth = 4.0;
  83.     Options.PicHeight = 3.0;
  84.     Options.Preview=1;
  85.     StdBGI=Options.StdBGI=0;
  86.     Options.PrinterNo = Options.PRTModeNo = -1;
  87.     appDriver = DETECT;
  88.     appMode = 0;
  89.     if (graphAppInit(appDriver, appMode, Options.bgiPath, True) == False)
  90.     messageBox("Cannot load graphics driver.", mfError | mfOKButton);
  91.     PRT_LinkDrivers();
  92. }
  93.  
  94. /*================*/
  95. TBGIApp::~TBGIApp()
  96. /*================*/
  97. {
  98.     graphAppDone();
  99. }
  100.  
  101. /*===================*/
  102. void TBGIApp::newWin()
  103. /*===================*/
  104. {
  105.     static ushort winNum = 0;
  106.     TWindow *p;
  107.  
  108.     TRect r(deskTop->getExtent());
  109.     r = TRect( winNum % (deskTop->size.y-1),
  110.                winNum % (deskTop->size.y-1),
  111.                deskTop->size.x,
  112.                deskTop->size.y);
  113.  
  114.     char msgStr[10];
  115.     ostrstream os( msgStr, 10 );
  116.     os << "Window " << winNum << ends;
  117.     p = new TWindow( r, msgStr, 0 );
  118.     p->options |= ofTileable;
  119.     deskTop->insert( validView(p) );
  120.     winNum++;
  121. }
  122.  
  123. int ScreenPreview=1;
  124. /*========================================================================*/
  125. void TBGIApp::doGraphics(int far Graphfunc(void far * UserPointer), void far* UserPointer)
  126. /*========================================================================*/
  127. {
  128.     char errorMsg[MAXSIZE];
  129.     ushort    maxX,maxY;
  130.     static const int CLIP_ON=1;
  131.  
  132.  
  133.    if ( Options.StdBGI )
  134.    {  // Std. BGI driver and std. Borland example code
  135.       suspend();
  136.       if (graphicsStart() == False)
  137.        {
  138.          resume();
  139.           strcpy(errorMsg,grapherrormsg(graphresult()));
  140.           strcat( errorMsg,"." );
  141.           messageBox(errorMsg, mfError | mfOKButton);
  142.        }
  143.        else
  144.        {
  145.           maxX = getmaxx();
  146.           maxY = getmaxy();
  147.           outtextxy(0, (maxY - textheight("M")),
  148.               "Press any key to return...");
  149.           setviewport(0, 0, maxX - 1, (maxY - (textheight("M") + 5)), CLIP_ON);
  150.  
  151.          Graphfunc(UserPointer);
  152.  
  153.           graphicsStop();
  154.          resume();
  155.        }
  156.    }
  157.    else
  158.    {
  159.       static int   PRTdrv=DETECT,PRTmode;
  160.       int          ReturnCode;
  161.       unsigned     oldhelpCtx;
  162.       PRT_LinkDrivers(); /* link drivers definitions into an executable code */
  163.       ReturnCode=PRT_SetDriver ( Options.PrinterNo+1, Options.PRTModeNo,
  164.                                  1000*Options.PicWidth,  1000*Options.PicHeight,
  165.                                  PRT_INVERSE );
  166.       if ( ReturnCode )
  167.             messageBox("Wrong printer or mode specified", mfError | mfOKButton);
  168.       else
  169.       {
  170.          ScreenPreview=Options.Preview;
  171.          PRT_SetOutName(Options.PrintDest);
  172.          PRT_SetUserPrintFunc(TVUserPrtFunc);
  173.          if ( PRTdrv==DETECT )
  174.          {
  175.             PRTmode=0;
  176.             /* If you want you may not linkining in the BitImage BGI driver into the */
  177.             /* EXE file. To do comment out following two lines. */
  178.             PRTdrv = PRT_installuserdriver ( "BitImage", NULL );
  179.             PRT_registerfarbgidriver ( BitImage );
  180.             PRT_SetHaltVariable((unsigned char *)&TSystemError::ctrlBreakHit);
  181.          }
  182.          oldhelpCtx = helpCtx;
  183.          helpCtx = hcGraphBuild;
  184.          TProgram::application->statusLine->update(); // change status line
  185.          TSystemError::ctrlBreakHit=0;     // reset break indicator
  186.          ReturnCode = PRT_PrintBGI ( &PRTdrv,&PRTmode,Options.bgiPath,
  187.                                      Graphfunc, UserPointer );
  188.          helpCtx = oldhelpCtx;
  189.          TProgram::application->statusLine->update();
  190.          if ( ReturnCode )
  191.              messageBox(PRT_grapherrormsg(ReturnCode), mfError | mfOKButton);
  192.       }
  193.    }
  194.  
  195. }
  196.  
  197. /*-------------------------------------------------*/
  198. void TEnumPrinter::newFocusedItem( ccIndex newItem )
  199. /*-------------------------------------------------*/
  200. {
  201.    TEnumValue::newFocusedItem(newItem);
  202.    if (EnumM!=NULL) EnumM->setModes(False);
  203. }
  204.  
  205. /*----------------------------------------------*/
  206. TEnumPrinter::TEnumPrinter(const TRect& bounds ):
  207.    TEnumValue(bounds,32)
  208. /*----------------------------------------------*/
  209. {  int maxl=0;
  210.     EnumM=NULL;
  211.     TStrCollection *PrinterNames= new TStrCollection ( PRT_MaxDriver(),PRT_MaxDriver() );
  212.     for ( int i=1; i<=PRT_MaxDriver(); i++ )
  213.     {   const char*s;
  214.         PRT_DriverName(i,&s);
  215.         if ( maxl<strlen(s) ) maxl=strlen(s);
  216.         PrinterNames->insert(newStr(s));
  217.     }
  218.    newList(PrinterNames,-1);
  219. }
  220.  
  221. /*-----------------------------------------------------------------*/
  222. TEnumMode::TEnumMode ( const TRect& bounds, const TEnumPrinter *EP ):
  223. /*-----------------------------------------------------------------*/
  224.    TEnumValue( bounds,32 )
  225. {
  226.    EnumP=EP;
  227.    setModes(False);
  228. };
  229.  
  230. /*------------------*/
  231. Boolean TEnumMode::setModes(Boolean errMsg)
  232. /*------------------*/
  233. {  int p;
  234.    p = EnumP->focusedItem;
  235.     if ( p<0 || p>=PRT_MaxDriver() )
  236.     {
  237.        if ( errMsg )
  238.             messageBox("You must specify printer first", mfError | mfOKButton);
  239.        return False;
  240.     }
  241.     else
  242.     {
  243.        int maxmode;
  244.        PRT_MaxMode(p+1,&maxmode);
  245.        TStrCollection *ModeNames= new TStrCollection ( maxmode+1,maxmode+1 );
  246.        for ( int i=0; i<=maxmode; i++ )
  247.        {   const char*s;
  248.            PRT_ModeName(p+1,i,&s);
  249.            ModeNames->insert(newStr(s));
  250.        }
  251.        newList(ModeNames,maxmode);
  252.        return True;
  253.     }
  254. }
  255.  
  256. /*----------------------------------------------*/
  257. void TEnumMode::MakeSelection()
  258. /*----------------------------------------------*/
  259. {
  260.    if ( setModes(True) )
  261.       TEnumValue::MakeSelection();
  262. }
  263.  
  264. /*=======================*/
  265. void TBGIApp::SetGraphOptions()
  266. /*=======================*/
  267. {
  268.  
  269.     TDialog *d = new TDialog(TRect(0,0,55,17), "Graph options");
  270.